/*
 * Created by: 
 * Created: Thursday, February 22, 2007
 */

using System;
using System.Web;
using Edge.Common.DataUtils;
using Edge.Common.Utils;
using Edge.Common.Utils;

namespace Edge.Common.Web.DataUtils
{
    public class SessionObject<T>
        where T : class, new()
    {
        private string key;
        private T fakeValue;

        public SessionObject(string key)
        {
            if (StringUtils.IsBlankOrNull(key))
                throw new ArgumentException("key");
            this.key = key;
        }

        public T Value
        {
            get
            {
                HttpContext context = HttpContext.Current;

                if (context == null)
                    throw new InvalidOperationException("http context does not exists");

                // web service call
                //if (context.Session == null)
                //{
                //    if (fakeValue == null)
                //        fakeValue = new T();
                //    return fakeValue;
                //}


                if (context.Session.Contents[key] == null)
                    context.Session.Contents[key] = new T();

                return (T)context.Session.Contents[key];
            }
        }
    }

}